# coding: utf-8

import string, re
from time import strptime

class Validator():
    def __init__(self):
        pass
    
    # fieldDic = { "typeName": "val" or ["val"] }
    def generalValidator(self, fieldDic):
        for type in fieldDic:
            val = fieldDic[type]
            if type == "username":
                if val == "":
                    print "Username shouldn't be blank"
                    return False
                else:
                    return True
            elif type == "password":
                if val[0] == "":
                    print "Password shouldn't be blank"
                    return False
                elif len(val) == 2 and val[0] != val[1]:
                    print "Confirm Password and Password do not match each other!!"
                    return False
                else:
                    return True
            elif type == "otherUsername":
                if val[0] == "":
                    print "Other Username shouldn't be blank"
                    return False
                
                otherUsernameList = val[0].split(",")
                for x in range(len(otherUsernameList)):
                    otherUsernameList[x] = string.strip(otherUsernameList[x])
                    
                authenticatedUser = val[1]["username"]
                #print otherUsernameList
                #print authenticatedUser
                if authenticatedUser in otherUsernameList:
                    print "Your username shouldn't in Other Username field"
                    return False
                else:
                    haveDuplicate = False
                    for x in range(len(otherUsernameList)):
                        if otherUsernameList.count(otherUsernameList[x]) > 1:
                            haveDuplicate = True
                            print "There are duplicate username in Other Username field"
                            break
                    
                    if haveDuplicate == True:
                        return False
                    else:
                        return True
            elif type == "time":
                #val = "1234-56-78 90:01:12"
                matchObj = re.match(u"(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})", val)
                if matchObj:
                    try:
                        strptime(val, "%Y-%m-%d %H:%M:%S")
                    except ValueError:
                        print "Time pattern error"
                        return False
                    print "Time pattern OK"
                    return True
                else:
                    print "Time pattern error"
                    return False
            elif type == "title":
                #val = "Hello World"
                if val != "":
                    return True
                else:
                    print "Title shouldn't be blank"
                    return False
            elif type == "confirm":
                if val in ["Yes", "No"]:
                    return True
                else:
                    print "Invalid input"
                    return False
    
    # fieldDic = { "typeName": resNum }
    def speicalValidator(self, fieldDic):
        for type in fieldDic:
            resNum = fieldDic[type]["resNum"]
            if type == "username":
                if resNum == 0:
                    print "User registration is successful"
                    return True
                elif resNum == 1:
                    print "Username already exists"
                    return False
            elif type == "userAuthentication":
                if resNum == 0:
                    print "Username or Password error"
                    return False
                elif resNum == 1:
                    print "Authentication succeed"
                    return True
            elif type == "otherUsernameIsExist":
                if resNum == 0:
                    print "Other Username total exist"
                    return True
                elif resNum == 1:
                    notExistUsernameList = fieldDic[type]["returnVal"]["notExistUsernameList"]
                    print "Other Username field has not exist username:"
                    for username in notExistUsernameList:
                        print username
                    
                    return False
            elif type == "addMeeting":
                if resNum == 0:
                    print "No agenda conflict in your meeting"
                    print "Meeting successfully added"
                    return True
                elif resNum == 1:
                    conflictUsernameList = fieldDic[type]["returnVal"]["conflictUsernameList"]
                    print "Failed to add meeting"
                    print "Agenda conflict username:"
                    for username in conflictUsernameList:
                        print username
                        
                    return False
            elif type == "queryMeeting":
                if resNum == 0:
                    print "No agenda related to you"
                    return False
                elif resNum == 1:
                    queryUserAgendaList = fieldDic[type]["returnVal"]["queryUserAgendaList"]
                    print "Searched agenda:"
                    for queryUserAgenda in queryUserAgendaList:
                        print "------------------------------------------------"
                        print "| Meeting ID:", queryUserAgenda[0]
                        print "| Start Time: " + queryUserAgenda[1]
                        print "|  End Time : " + queryUserAgenda[2]
                        print "|   Title   : " + queryUserAgenda[3]
                        print "| Organizer : " + queryUserAgenda[4]
                        print "------------------------------------------------"
                        
                    return True
            elif type == "deleteMeeting":
                if resNum == 0:
                    print "Meeting doesn't exist"
                    return False
                elif resNum == 1:
                    print "This meeting isn't organized by you"
                    return False
                elif resNum == 2:
                    print "The meeting was successfully deleted"
                    return True
            elif type == "clearMeeting":
                if resNum == 0:
                    print "No meeting need to clear"
                    return False
                elif resNum == 1:
                    print "The meeting was successfully cleared"
                    return True
                    
                    